GBML manual / Maths

int_round

The nearest int to value - 1.4 becomes 1, 1.5 becomes 2, and -1.5 becomes -2 (an exact tie rounds away from zero, matching the fixed-point type’s own nearest-whole-number rounding).

Syntax

int_round(value) -> int

Arguments

NameType
valuereal

Returns

int

Description

The nearest int to value - 1.4 becomes 1, 1.5 becomes 2, and -1.5 becomes -2 (an exact tie rounds away from zero, matching how a real number line splits evenly around each half-way point).

Use this for "which of several discrete choices is value closest to" - snapping a continuous angle to the nearest 45-degree facing, or a volume slider to the nearest of 10 steps. It is not the right choice for "which cell/tile contains this position" (that always wants int_floor instead, even though the two agree at first glance for a positive value) - rounding to the nearest cell BOUNDARY is a different question from finding which cell a position is standing inside, and the two disagree for roughly half of every cell.

Example

var facing_angle = 100; // degrees, 0 = east, counterclockwise
var facing = int_round(facing_angle / 45);

facing_angle / 45 is 2.222... as a real - int_round snaps it to the int 2, the nearest of 8 possible facings (0-7) for a character that only aims in 45-degree steps, rather than the 2.222... steps it is actually partway toward.

On the GBA

For "which of several discrete slots is this continuous value closest to" - bucketing a continuous aim angle into whichever of 8 facings it is nearest is exactly this. Not for grid/tile maths (that rounds the wrong way half the time versus the cell the position is actually inside) - see int_floor for that.

See also: roundint_partint_floorint_ceil